package org.ithang.application.msg;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.ithang.application.customer.bean.Customer;
import org.ithang.application.msg.bean.Msg;
import org.ithang.application.msg.service.MsgService;
import org.ithang.system.user.bean.User;
import org.ithang.tools.lang.DateUtils;
import org.ithang.tools.model.Action;
import org.ithang.tools.model.ActionResult;
import org.ithang.tools.model.ActionValues;
import org.ithang.tools.model.ErrorInfo;
import org.ithang.tools.model.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 站内留言
 * @author zyt 控制器
 *
 */
@Controller
@RequestMapping("/app/msg")
public class MsgAction extends Action<Msg>{

    @Autowired
    private MsgService msgService; 


    @ResponseBody
	@RequestMapping(value="add",method=RequestMethod.POST)
	public ActionResult add(Msg msg){
		msgService.add(msg);
		return success();
	}
	
	 @RequestMapping("form")
    public String form(@RequestParam(value="id",required=false)Long id,Model m){
    	if(null!=id){
    		Msg bean=msgService.get(id);
    		m.addAttribute(bean);
    	}else{
    		m.addAttribute(new Msg());
    	}
    	return "app/msg/form";
    }
	
	
	@ResponseBody
	@RequestMapping(value="update",method=RequestMethod.POST)
	public ActionResult update(Msg msg){
		msgService.update(msg);
		return success();
	}
	
	/**
	 * 发送站内信
	 * @param msg
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="send",method=RequestMethod.POST)
	public ActionResult send(Msg msg){
		
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isRemembered()||subject.isAuthenticated()){
    		User user=(User)subject.getPrincipal();
    		msg.setCreate_time(DateUtils.getSystime());
    		msg.setCustomer_id(user.getId());
    		msg.setUpdate_time(DateUtils.getSystime());
    		msgService.update(msg);	
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
		
		return success();
	}
	
	/**
	 * 回复
	 * @param msg
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="reply",method=RequestMethod.POST)
	public ActionResult reply(Msg msg){
		Subject subject = SecurityUtils.getSubject();
    	if(subject.isRemembered()||subject.isAuthenticated()){
    		User user=(User)subject.getPrincipal();
    		msg.setAdmin_id(user.getId());
    		msg.setUpdate_time(DateUtils.getSystime());
    		msgService.update(msg);	
    	}else{
    		return error(ErrorInfo.NoLoginError);
    	}
		return success();
	}


    @ResponseBody
	@RequestMapping(value="get",method=RequestMethod.GET)
	public ActionResult get(Long id){
		Msg r=msgService.get(id);
		return success(r);
	}
	
	@ResponseBody
	@RequestMapping(value="delete",method=RequestMethod.POST)
	public ActionResult delete(@RequestParam(value="id",required=false)Long id,@RequestParam(value="ids",required=false)String ids){
		
		if(null!=id&&id>0){
			msgService.delete(id);	
		}
		
		if(null!=ids&&ids.length()>0){
			msgService.batchDelete(ids.split(","));
		}
		
		return success();
	}
	
	@ResponseBody
	@RequestMapping(value="list",method={RequestMethod.GET,RequestMethod.POST})
	public ActionResult list(Long... ids){
		return success(msgService.list(ids));
	}
	
	@ResponseBody
	@RequestMapping(value="page",method=RequestMethod.POST)
	public ActionResult page(Msg msg,Pager<Msg> page){
	    page.setBean(msg);
	    List<Msg> data=msgService.page(page);
	    page.setData(data);
		return success(page);
	}
	
	@ResponseBody
	@RequestMapping(value="query",method=RequestMethod.POST)
	public ActionResult query(HttpServletRequest request){
		ActionValues values=new ActionValues(request);
		return success(msgService.query(values));
	}
     

}